/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.io;

import java.io.Writer;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.Validate;
import org.jfab.core.collection.CGAnnotationList;
import org.jfab.core.collection.CGParameterList;
import org.jfab.core.collection.CGTypeList;
import org.jfab.core.collection.CommentList;
import org.jfab.core.collection.MethodBodyList;
import org.jfab.core.collection.ModifierList;
import org.jfab.core.factory.StringFactory;
import org.jfab.core.model.CGConstructor;

/**
 * Provides a constructor writer.
 */
public final class CGConstructorWriter implements CGWriter<CGConstructor>
{
    /** Basic code generator. */
    private final BasicCodeGenerator _basicCodeGenerator;

    /** String factory. */
    private final StringFactory _stringFactory;

    /**
     * Construct this object with the given parameters.
     * 
     * @param stringFactory String factory.
     * @param basicCodeGenerator Basic code generator.
     */
    public CGConstructorWriter(final StringFactory stringFactory,
            final BasicCodeGenerator basicCodeGenerator)
    {
        _stringFactory = stringFactory;
        _basicCodeGenerator = basicCodeGenerator;
    }

    /**
     * Write a constructor.
     * 
     * @param writer Writer.
     * @param constructor Constructor.
     */
    @Override
    public final void write(final Writer writer, final CGConstructor constructor)
    {
        Validate.notNull(constructor, "constructor is null");

        final CommentList commentLines = new CommentList(
                constructor.getCommentLines());
        final CGAnnotationList annotations = new CGAnnotationList(
                constructor.getAnnotations());
        final ModifierList modifiers = new ModifierList(
                constructor.getModifiers());
        final String className = constructor.getName();
        final CGParameterList parameters = new CGParameterList(
                constructor.getParameters());
        final CGTypeList throwsTypes = new CGTypeList(
                constructor.getThrowsTypes());
        final MethodBodyList methodBodyLines = new MethodBodyList(
                constructor.getMethodBodyLines());

        getBasicCodeGenerator().writeMultilineJavadocComment(writer,
                commentLines);
        writeConstructorDeclaration(writer, annotations, modifiers, className,
                parameters, throwsTypes);
        writeMethodBody(writer, methodBodyLines);
    }

    /**
     * @return the basicCodeGenerator
     */
    private BasicCodeGenerator getBasicCodeGenerator()
    {
        return _basicCodeGenerator;
    }

    /**
     * @return the stringFactory
     */
    private StringFactory getStringFactory()
    {
        return _stringFactory;
    }

    /**
     * Write a method declaration.
     * 
     * @param writer Writer.
     * @param annotations Annotations.
     * @param modifiers Modifiers (@see java.lang.reflect.Modifier)
     * @param className Class name. (required)
     * @param parameters Parameters.
     * @param throwsTypes Throws types.
     */
    private void writeConstructorDeclaration(final Writer writer,
            final CGAnnotationList annotations, final ModifierList modifiers,
            final String className, final CGParameterList parameters,
            final CGTypeList throwsTypes)
    {
        Validate.notEmpty(className, "className is empty");

        final StringFactory stringFactory = getStringFactory();

        final String annotationsString = stringFactory
                .createAnnotationsString(annotations);
        final String modifiersString = stringFactory
                .createModifiersString(modifiers);
        final String parametersString = stringFactory
                .createParametersString(parameters);
        final String throwsString = stringFactory
                .createThrowsString(throwsTypes);

        final StringBuilder sb = new StringBuilder();
        sb.append(annotationsString);
        sb.append(modifiersString);
        sb.append(className);
        sb.append("(");
        sb.append(parametersString);
        sb.append(")");
        sb.append(throwsString);

        getBasicCodeGenerator().writeLine(writer, sb.toString());
    }

    /**
     * @param writer Writer.
     * @param methodBodyLines Method body lines.
     */
    private void writeMethodBody(final Writer writer,
            final MethodBodyList methodBodyLines)
    {
        final BasicCodeGenerator basicCodeGenerator = getBasicCodeGenerator();
        basicCodeGenerator.writeOpenBrace(writer);

        if (CollectionUtils.isNotEmpty(methodBodyLines))
        {
            final int size = methodBodyLines.size();

            for (int i = 0; i < size; i++)
            {
                basicCodeGenerator.writeLine(writer, methodBodyLines.get(i));
            }
        }

        basicCodeGenerator.writeCloseBrace(writer);
    }
}
